package com.corn.kafkastream.serdes;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.kafka.common.serialization.Deserializer;

import java.io.IOException;

/**
 * @author : Jim Wu
 * @version 1.0
 * @function :
 * @since : 2022/9/20 17:47
 */

public class JsonDeSerializer<T> implements Deserializer<T> {
    private static final ObjectMapper OBJECT_MAPPER;

    private final Class<T> clzz;


    static {
        OBJECT_MAPPER = new ObjectMapper();
        OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }

    public JsonDeSerializer(Class<T> clzz) {
        this.clzz = clzz;
    }

    @Override
    public T deserialize(String topic, byte[] data) {
        try {
            return OBJECT_MAPPER.readValue(data, this.clzz);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
}
